﻿using ESPL.CodeGenerator.Common;
using ESPL.CodeGenerator.Common.ProjectBuilders;
using System;
using ESPL.CodeGenerator.Domain.TemplateGenerators.CSharpCodeGenerators;
using ESPL.CodeGenerator.Common.Structs;
using System.Collections.Generic;
using ESPL.CodeGenerator.Common.Generators;
using ESPL.CodeGenerator.Domain.Utilities;

namespace ESPL.CodeGenerator.Domain.ProjectBuilders
{
    internal abstract class ProjectBuilderBase : IProjectBuilder
    {
        #region Declarations

        private ICodeBuilder _successor = null;
        private ApplicationConfiguration _config;
        protected IBaseGenerator _generator;

        #endregion

        #region Constructor

        public ProjectBuilderBase(ApplicationConfiguration config)
        {
            _config = config;
        }

        #endregion

        #region Methods

        public virtual IList<IBuilderResponse> ProcessRequest(IProjectBuilderRequest request)
        {
            var response = new List<IBuilderResponse>();
            InitGenerator(request);
            
            if(CreateDir)
                DirectoryHelper.CreateDirectory(GetDirectoryPath());
            if(WriteFile)
                FileHelper.CreateFile(GetFilePath(), _generator.TransformText());

            response.Add(GetResponse());
            if (Successor != null)
                response.AddRange(Successor.ProcessRequest(request));
            return response;
        }

        protected abstract void InitGenerator(IProjectBuilderRequest request);

        protected abstract BuilderResponse GetResponse();

        protected virtual string GetDirectoryPath()
        {
            return string.Empty;
        }

        protected virtual string GetFilePath()
        {
            return string.Empty;
        }

        #endregion

        #region Properties

        public ICodeBuilder Successor
        {
            get
            {
                return _successor;
            }
            set
            {
                _successor = value;
            }
        }

        protected ApplicationConfiguration Config
        {
            get
            {
                return _config;
            }
        }

        protected bool CreateDir { get; set; }

        protected bool WriteFile { get; set; }

        #endregion
    }
}
